package com.lxw.nonghe.config;

import com.lxw.nonghe.interceptor.AuthInterceptor;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    private final AuthInterceptor authInterceptor;

    public WebMvcConfig(AuthInterceptor authInterceptor) {
        this.authInterceptor = authInterceptor;
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(authInterceptor)
                /*.addPathPatterns("/api/**") // 拦截/api开头的请求
                .excludePathPatterns("/api/auth/login"); // 排除登录接口*/
                .addPathPatterns("/**") // 拦截/api开头的请求
                .excludePathPatterns("/auth/login","/user/create"); // 排除登录接口和添加用户
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
/*        // 先配置Swagger资源
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");*/

        // 再配置静态资源
        registry.addResourceHandler("/**")
                .addResourceLocations("classpath:/static/");
    }
}
